//
//  PoseThumbnailPagerViewCell.swift
//  SitPosition
//
//  Created by apple on 2019/2/9.
//  Copyright © 2019 apple. All rights reserved.
//

import Foundation
import FSPagerView


/**
 * 坐姿快照缩略图Cell
 */
class PoseThumbnailPagerViewCell : FSPagerViewCell {
    
    // MARK: - Properties
    
    // 渐变图层
    fileprivate lazy var gradientLayer: CAGradientLayer = {
        let layer = CAGradientLayer()
        layer.colors = [
            UIColor.black.withAlphaComponent(0.0).cgColor,
            UIColor.black.withAlphaComponent(1.0).cgColor
        ]
        layer.type = CAGradientLayerType.axial
        layer.startPoint = CGPoint(x: 0.5, y: 0)
        layer.endPoint = CGPoint(x: 0.5, y: 1)
        layer.locations = [0.0, 1.0]
        layer.opacity = 0.6
        return layer
    }()
    
    // MARK: - Object lifecycle
    
    override init(frame: CGRect) {
        super.init(frame: frame)
        _commonInit()
    }
    
    required init?(coder aDecoder: NSCoder) {
        super.init(coder: aDecoder)
        _commonInit()
    }
    
    fileprivate func _commonInit() {
        contentView.layer.addSublayer(gradientLayer)
        textLabel?.font = Theme.Font.boldFont(ofSize: 11)
        textLabel?.textAlignment = NSTextAlignment.center
    }
    
    override func layoutSubviews() {
        let bounds: CGRect = contentView.bounds
        
        let gradientLayerH: CGFloat = 20
        gradientLayer.frame = CGRect(x: 0, y: bounds.height - gradientLayerH,
                                     width: bounds.width, height: gradientLayerH)
        
        imageView?.layer.cornerRadius = 2
        imageView?.layer.masksToBounds = true
        imageView?.frame = CGRect(x: 0, y: 0, width: bounds.width, height: bounds.height)
        
        textLabel?.sizeToFit()
        let height: CGFloat = textLabel?.bounds.height ?? 0
        textLabel?.frame = CGRect(x: 4, y: bounds.height - height,
                                  width: bounds.width - 2*4, height: height)
        if let label = textLabel {
            label.superview?.bringSubviewToFront(label)
        }
        if let iv = imageView {
            iv.superview?.sendSubviewToBack(iv)
        }
    }
}
